package com.demo.webmagic.pageProcessor;

import java.util.ArrayList;
import java.util.List;

import com.demo.common.constant.ConstantDtoFlight;
import com.demo.common.model.Proxyip;
import com.demo.webmagic.dto.DtoProxyIp;
import com.jfinal.log.Log;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

public class PageProcessorKuaidaili implements PageProcessor {

	private Site site = Site.me().setRetryTimes(3).setSleepTime(5000).setCycleRetryTimes(3);
	Log log = Log.getLog(DtoProxyIp.class);

	@Override
	public Site getSite() {
		return site;
	}

	@Override
	public void process(Page page) {
		DtoProxyIp dtoProxyIp = new DtoProxyIp();
		try {
			List<Proxyip> proxyIpList = new ArrayList<Proxyip>();
			List<Selectable> trList = page.getHtml().xpath("//tbody/tr").nodes();
			for (Selectable tr : trList) {
				List<Selectable> tdList = tr.xpath("//td").nodes();
				Proxyip proxyip = new Proxyip();
				proxyip.setIpStr(tdList.get(0).$("td", "text").toString().trim());
				proxyip.setPortStr(tdList.get(1).$("td", "text").toString().trim());
				proxyip.setLocation(tdList.get(4).xpath("//td/a[1]/text()").toString().trim());
				proxyip.setValiDate(tdList.get(6).$("td", "text").toString().trim());
				proxyIpList.add(proxyip);
				/*System.out.println(tdList.get(4).xpath("//td/a[1]/text()").toString() + " " + tdList.get(4).xpath("//td/a[2]/text()").toString());
				for (Selectable td : tdList) {
					System.out.println(td.$("td", "text").toString().trim());
				}
				System.out.println("+++++++++++++++++++");*/
			}
			dtoProxyIp.setCode(ConstantDtoFlight.code_success);
			dtoProxyIp.setProxyIpList(proxyIpList);
		} catch (Exception e) {
			log.error(e.getMessage());
			dtoProxyIp.setCode(ConstantDtoFlight.code_fail);
		} finally {
			page.putField("result", dtoProxyIp);
		}
	}

}
